﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;

namespace CSAutomateExcel
{
    static class Solution3
    {
        public static void enumerateFiles()
        {
            //
            object missing = Type.Missing;
            // enumberate files
            string sCurdir = Directory.GetCurrentDirectory();
            string[] saFiles = Directory.GetFiles(sCurdir);
            // excel compose
            try
            {
                Excel.Application oXL = new Excel.Application();
                oXL.Visible = true;
                Console.WriteLine("");

                // Create a new Workbook.
                Excel.Workbook oWB = oXL.Workbooks.Add(missing);
                Console.WriteLine("A new workbook is created");

                // Get the active Worksheet and set its name.
                Excel.Worksheet oSheet = oWB.ActiveSheet as Excel.Worksheet;
                oSheet.Name = "目录";
                Console.WriteLine("The active worksheet is renamed as Report");

                // file data into the wordsheet's cells
                // set set column header
                oSheet.Cells[1, 1] = "AA";
                oSheet.Cells[1, 2] = "BB";
                oSheet.Cells[1, 3] = "CC";

                //oSheet.get_Range("B:B").Value2 = saFiles;
                oSheet.get_Range("B2").Value2 = saFiles;

                for (int i = 0; i < saFiles.Count(); i++)
                {
                    oSheet.Cells[i+2, 2] = saFiles[i];
                    oSheet.Hyperlinks.Add(oSheet.Cells[i + 2, 2], saFiles[i]);
                }

                    // add hyper link @ A7
                    oSheet.Hyperlinks.Add(oSheet.Cells[7, 1], "c:\\");

                // Save the workbook as a xlsx file and close it.

                Console.WriteLine("Save and close the workbook");

                string fileName = "目录.xlsx";
                oWB.SaveAs(fileName, Excel.XlFileFormat.xlOpenXMLWorkbook,
                    missing, missing, missing, missing,
                    Excel.XlSaveAsAccessMode.xlNoChange,
                    missing, missing, missing, missing, missing);
                //oWB.Close(missing, missing, missing);

                // Quit the Excel application.

                Console.WriteLine("Quit the Excel application");

                // Excel will stick around after Quit if it is not under user 
                // control and there are outstanding references. When Excel is 
                // started or attached programmatically and 
                // Application.Visible = false, Application.UserControl is false. 
                // The UserControl property can be explicitly set to True which 
                // should force the application to terminate when Quit is called, 
                // regardless of outstanding references.
                oXL.UserControl = true;

                //oXL.Quit();
            }
            catch (Exception ex)
            {
 
            }

            
        }
    }
}
